package hc.bre.calc

import static org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

import hc.bre.calc.scorecard.*
import hc.bre.clas.range.RangeBuilder
import hc.bre.rest.manage.MainService
import hc.bre.runner.ScriptLoaderRunner
import java.lang.reflect.Field

@SpringBootTest
class ScorecardTest {

	@Test
	void create() {
		ScBranchItem clasItem = new ScBranchItem();

		ScLeafItem calcItem = new ScLeafItem();

		ScorecardDefinition ruleDef = new ScorecardDefinition();
	}

	String jsonText ='''
{
  "rid" : 285,
  "rname" : "BasicScorecard",
  "rpath" : "xsxdjc/BasicScorecard",
  "rversion" : 57,
  "rmaxid" : 19,
  "rmemo" : "",
  "rtest" : "",
  "score" : 600,
  "transform" : 4,
  "list" : [ {
    "id" : 12,
    "referId" : "xsxdjc/class9/年龄风险分类",
    "referName" : "年龄风险分类",
    "list" : [ {
      "id" : 13,
      "referId" : 1,
      "referName" : "青年",
      "score" : 100,
      "reasonId" : 1,
      "userdef" : "1"
    }, {
      "id" : 14,
      "referId" : 2,
      "referName" : "壮年",
      "score" : 200,
      "reasonId" : 2,
      "userdef" : "2"
    }, {
      "id" : 15,
      "referId" : 3,
      "referName" : "中年",
      "score" : 50,
      "reasonId" : 3,
      "userdef" : "3"
    }, {
      "id" : 16,
      "referId" : 0,
      "referName" : "其它",
      "score" : 0,
      "reasonId" : 4,
      "userdef" : "4"
    } ]
  }, {
    "id" : 17,
    "referId" : "xsxdjc/class9/是否优质画像",
    "referName" : "是否优质画像",
    "list" : [ {
      "id" : 18,
      "referId" : 1,
      "referName" : "yes:是",
      "score" : 40,
      "userdef" : "5"
    }, {
      "id" : 19,
      "referId" : 0,
      "referName" : "no:否",
      "score" : 20,
      "userdef" : "6"
    } ]
  } ],
  "rtype" : "rule:calc:scorecard"
}
'''

	@Test
	void load() {
		ScorecardDefinition ruleDef = new ScorecardDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));
	}

	@Test
	void buildAndEvaluate() {
		ScorecardDefinition ruleDef = new ScorecardDefinition().load(jsonText);
		ScBuilder builder = new ScBuilder() {
			@Override
			public String _applyClas(String rpath) {
				return "[clas:[id:1,code:'clas1']]"
			}

			@Override
			public String _referReason(Integer reasonId) {
				return "[id:reasonId,code:'reason${reasonId}',name:'']";
			}

			@Override
			public String _referTranform() {
				return "{it*2}";
			}
		};
		
		ScriptLoaderRunner runner = ScriptLoaderRunner.forRule(ruleDef,builder);
		def ret= runner.run()
//		println hc.Util.jsonWrite(ret);
		assert ret.result.score == 1480;
		assert ret.result.detail[0].score == 100;
		assert ret.result.detail[0].userdef == 1;
		assert ret.result.detail[0].reasonCode == 'reason1';
		assert ret.result.detail[0].clasCode == 'clas1';
		
	}

//	@Test TEMPXYZ
	void buildAndEvaluateWithContext() {
		ScorecardDefinition ruleDef = new ScorecardDefinition().load(jsonText);
		ScBuilder builder = new ScBuilder();
		ScriptLoaderRunner runner = ScriptLoaderRunner.forRule(ruleDef,builder);
		def ret= runner.run("基本信息": ["年龄": 20,"教育": "本科"])
		assert ret.result.score == 814;
	}
}
